package com.tomcat.myapplication;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tomcat.myapplication.adapter.QueryAdapter;
import com.tomcat.myapplication.bean.AdapterData;
import com.tomcat.myapplication.bean.DataBean;
import com.tomcat.myapplication.bean.ResultInfo;
import com.tomcat.myapplication.databinding.ActivityMainBinding;
import com.tomcat.myapplication.presenter.MainPresenter;
import com.tomcat.myapplication.util.BackHandlerHelper;
import com.tomcat.myapplication.util.DateAlert;
import com.tomcat.myapplication.util.DateTimeUtil;
import com.tomcat.myapplication.util.ToastUtil;
import com.tomcat.myapplication.view.iView.IMainActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IMainActivity {

    private Context context;

    private ActivityMainBinding binding;

    private QueryAdapter checkSunAdapter;
    private QueryAdapter carAdapter;
    private QueryAdapter cablewayAdapter;
    private QueryAdapter normalAdapter;
    private QueryAdapter blueAdapter;
    private MainPresenter presenter;
    private long lastBackPress;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setViewclick(new ViewClick());
        context = this;
        binding.tvDate.setText(DateTimeUtil.getCurrentTime());
        presenter = new MainPresenter(this);
        presenter.getDriverfree(DateTimeUtil.getCurrentTime());
        checkSunAdapter = new QueryAdapter(new ArrayList<AdapterData>());
        binding.rvCheckSum.setLayoutManager(new LinearLayoutManager(context));
        binding.rvCheckSum.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        binding.rvCheckSum.setHasFixedSize(true);   //scrollview 嵌套recyclerview滑动卡顿问题
        binding.rvCheckSum.setNestedScrollingEnabled(false);
        binding.rvCheckSum.setAdapter(checkSunAdapter);

        carAdapter = new QueryAdapter(new ArrayList<AdapterData>());
        binding.rvCar.setLayoutManager(new LinearLayoutManager(context));
        binding.rvCar.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        binding.rvCar.setHasFixedSize(true);   //scrollview 嵌套recyclerview滑动卡顿问题
        binding.rvCar.setNestedScrollingEnabled(false);
        binding.rvCar.setAdapter(carAdapter);

        cablewayAdapter = new QueryAdapter(new ArrayList<AdapterData>());
        binding.rvCableway.setLayoutManager(new LinearLayoutManager(context));
        binding.rvCableway.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        binding.rvCableway.setHasFixedSize(true);   //scrollview 嵌套recyclerview滑动卡顿问题
        binding.rvCableway.setNestedScrollingEnabled(false);
        binding.rvCableway.setAdapter(cablewayAdapter);


        normalAdapter = new QueryAdapter(new ArrayList<AdapterData>());
        normalAdapter.setColumn(3);//指定三显示三列
        binding.rvNormal.setLayoutManager(new LinearLayoutManager(context));
        binding.rvNormal.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        binding.rvNormal.setHasFixedSize(true);     //scrollview 嵌套recyclerview滑动卡顿问题
        binding.rvNormal.setNestedScrollingEnabled(false);
        binding.rvNormal.setAdapter(normalAdapter);


        blueAdapter = new QueryAdapter(new ArrayList<AdapterData>());
        binding.rvBlue.setLayoutManager(new LinearLayoutManager(context));
        binding.rvBlue.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        binding.rvBlue.setHasFixedSize(true);     //scrollview 嵌套recyclerview滑动卡顿问题
        binding.rvBlue.setNestedScrollingEnabled(false);
        binding.rvBlue.setAdapter(blueAdapter);



    }

    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {
            if (System.currentTimeMillis() - lastBackPress < 1000) {
                super.onBackPressed();
            } else {
                lastBackPress = System.currentTimeMillis();
                Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
            }
        }
    }



    @Override
    public void getDriverfreeSuccess(DataBean dataBean) {
        //binding.contentLayout.setVisibility(View.VISIBLE);
        List<DataBean.CheckSumBean> sumBeanList = dataBean.getCheckSum();
        if(sumBeanList.size()!=0){
            //总计检人数
            binding.numall.setVisibility(View.VISIBLE);
            binding.numalls.setVisibility(View.VISIBLE);
            List<AdapterData> checkSumList = new ArrayList<>();
            DataBean.CheckSumBean checkSumBean = sumBeanList.get(0);
            checkSumList.add(new AdapterData("已售观光车票", checkSumBean.getRealSum()));
            checkSumList.add(new AdapterData("已检观光车票", checkSumBean.getCheckSum()));
            checkSumList.add(new AdapterData("未检观光车票", checkSumBean.getNotCheckSum()));
            checkSunAdapter.setNewData(checkSumList);   //刷新列表
        }else {
            binding.numall.setVisibility(View.GONE);
            binding.numalls.setVisibility(View.GONE);
            List<AdapterData> checkSumList = new ArrayList<>();
            checkSunAdapter.setNewData(checkSumList);   //刷新列表
        }
        //观光车, 索道
        List<DataBean.CarBean> catBeanList = dataBean.getCar();
        if (catBeanList.size() != 0) {
           binding.checknum.setVisibility(View.VISIBLE);
            binding.guodao.setVisibility(View.VISIBLE);
            binding.checknums.setVisibility(View.VISIBLE);
            binding.guodaos.setVisibility(View.VISIBLE);
            List<AdapterData> carList = new ArrayList<>();
            List<AdapterData> cablewayList = new ArrayList<>();
            for (int i = 0; i < catBeanList.size(); i++) {
                DataBean.CarBean item = catBeanList.get(i);
                if ("0".equals(item.getTerminalFlag())) {
                    carList.add(new AdapterData(item.getScenicSpotsShortName(), item.getCheckSum()));
                } else {
                    cablewayList.add(new AdapterData(item.getScenicSpotsShortName(), item.getCheckSum()));
                }
            }
            carAdapter.setNewData(carList);
            cablewayAdapter.setNewData(cablewayList);
        }else {
            binding.checknum.setVisibility(View.GONE);
            binding.guodao.setVisibility(View.GONE);
            binding.checknums.setVisibility(View.GONE);
            binding.guodaos.setVisibility(View.GONE);
            List<AdapterData> carList = new ArrayList<>();
            List<AdapterData> cablewayList = new ArrayList<>();
            carAdapter.setNewData(carList);
            cablewayAdapter.setNewData(cablewayList);
        }

        //车公司
        List<DataBean.NormalBean> normalBeanList = dataBean.getNormal();
        if (normalBeanList.size() != 0) {
            binding.chegongsi.setVisibility(View.VISIBLE);
            binding.chegongsis.setVisibility(View.VISIBLE);
            binding.layout.setVisibility(View.VISIBLE);
            List<AdapterData> normalList = new ArrayList<>();
            for (int i = 0; i < normalBeanList.size(); i++) {
                DataBean.NormalBean item = normalBeanList.get(i);
                normalList.add(new AdapterData(item.getTerminalName(), item.getCheckSum(), item.getStaffSum()));
            }
            normalAdapter.setNewData(normalList);
        }else {
            binding.chegongsi.setVisibility(View.GONE);
            binding.chegongsis.setVisibility(View.GONE);
            binding.layout.setVisibility(View.GONE);
            List<AdapterData> normalList = new ArrayList<>();
            normalAdapter.setNewData(normalList);
        }


        //蓝牙月谷
        List<DataBean.BlueBean> blueBeanList = dataBean.getBlue();
        if (blueBeanList.size() != 0) {
            binding.yuegu.setVisibility(View.VISIBLE);
            binding.yuegus.setVisibility(View.VISIBLE);
            List<AdapterData> blueList = new ArrayList<>();
            DataBean.BlueBean item = blueBeanList.get(0);
            blueList.add(new AdapterData("已售单点票", item.getRealSum()));
            blueList.add(new AdapterData("已检单点票", item.getCheckSum()));
            blueAdapter.setNewData(blueList);
        }else {
            binding.yuegu.setVisibility(View.GONE);
            binding.yuegus.setVisibility(View.GONE);
            List<AdapterData> blueList = new ArrayList<>();
            blueAdapter.setNewData(blueList);
        }
        if(sumBeanList.size()==0&&catBeanList.size()==0&&normalBeanList.size()== 0&&blueBeanList.size()== 0){
              if (getApplicationContext() != null) {
            View emptyView = getLayoutInflater().inflate(R.layout.nonelijiang, null);
            emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
                  normalAdapter.setEmptyView(emptyView);
        }
        }else {

        }



    }

    @Override
    public void getDriverfreeError(String msg) {
        ToastUtil.longToast(this, msg);

    }


    /**
     * 点击事件
     */
    public class ViewClick {
        public void onSelectDate() {
            new DateAlert(context, binding.tvDate).show();
        }

        public void onQuery() {
            presenter = new MainPresenter(MainActivity.this);
            String day = binding.tvDate.getText().toString();
            presenter.getDriverfree(day);

        }
    }

}
